package org.manage.service.impl;

import org.manage.dao.DepartmentDao;
import org.manage.dto.PageBean;
import org.manage.entity.Department;
import org.manage.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 逻辑层
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentDao departmentDao;

    @Override
    public List<Department> getAllDepartment() {
        return departmentDao.findAll();
    }

    @Override
    public PageBean<Department> getDepartmentByPage(int currPage, int pageSize) {
        PageBean<Department> pageBean = new PageBean<Department>();
        pageBean.setCurrPage(currPage);
        pageBean.setPageSize(pageSize);
        int totalCount = departmentDao.findCount();
        pageBean.setTotalCount(totalCount);
        double tc = totalCount;
        double num = Math.ceil(tc / pageSize);
        pageBean.setTotalPage((int) num);
        int begin = (currPage - 1) * pageSize;
        List<Department> lists = departmentDao.findByPage(begin, pageSize);
        pageBean.setList(lists);
        return pageBean;
    }

    @Override
    public void saveDepartment(Department department) {
        departmentDao.save(department);
    }

    @Override
    public Department getDepartmentById(int did) {
        return departmentDao.findById(did);
    }

    @Override
    public void updateDepartment(Department department) {
        departmentDao.update(department);
    }

    @Override
    public void deleteDepartment(int did) {
        departmentDao.delete(did);
    }
}
